import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2)));;
    }
    public static int[] topKFrequent(int[] nums, int k) {
        Integer num=null;
        Integer count=null;
        int index = 0;
        int[] values = new int[k];
        int[] counts = new int[k];
        for (int number :nums){
            if (num==null||num!=number){

                num=number;
                if (count!=null){
                    if (index<k){
                        counts[index]=count;
                        values[index]=num;
                        index++;
                    }else{
                        int minIndex = getMinCountIndex(counts);
                        System.out.println(minIndex);
                        counts[minIndex]=count;
                        values[minIndex]=num;
                    }
                }else {
                    counts[index]=1;
                    values[index]=num;
                    index++;
                }
                count=1;
            }else{
                count++;
            }
            System.out.println(count);
        }
        return values;
    }
    public static int getMinCountIndex(int[] counts){
        int min = Integer.MAX_VALUE;
        int index = 0;
        int minIndex=0;
        for(int count:counts){
            if (count<min){
                min = count;
                minIndex = index;
            }
            index++;
        }
        return minIndex;
    }
}